<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 12.10.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class clientNodeFolder
    ****
    * This classs represents a folder
    ****
    * @package clientNodes
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 12.10.2005
    * @copyright essentialmind.com 2005
    ***/
    class clientNodeGfolder extends clientNode{
        
        private $galleryImageType;
        
        /**
         * Process inputs
         * 
         * @return void 
         **/
        public function process(){           
            // first figure out the ID of the datatype responsible for gallery
            // images
            $this->setGalleryImageType();

            //first I need to get the IDs of all child nodes of this folder
            /*
                Please note, that a whole array of all child nodes is fetched as well,
                as it will be passed to smarty in order to present on the interface
                that there exist subfolders in the selected folder
            */
            $myTree     = new tree($this->db,'structure LEFT JOIN objectmap ON structure.id = objectmap.nodeid',$this->log,'id, level, path_cache AS path, title, published_pages');
            $nodes      = $myTree->getChildren($this->node);
            $children   = array();  // list of node IDs that lie below the current node
            $subcats    = array();  // list of direct subcategories i.e. no grandchildren
            
            //mark the level of the subcategories
            if(isset($nodes[0])){
                $curLevel   = $nodes[0]['level'];
            }
            
            $children[] = $this->node;  //add current node to the set limiter
            
            //loop through the node array and create associate markers to pass to smarty
            foreach($nodes as $node){
                $children[] = $node['id'];
                if($node['level'] == $curLevel){
                    $subcats[] = $node;   
                }
            }

            /*
                create the pager for this iFolder, in order to do this we need to
                first identify the total number of inserats there are in the
                currently selected folder
            */
            $total = $this->db->getOne("SELECT count(*)
                                                FROM pagemap AS pm 
                                                LEFT JOIN node_gallery_image AS ngi ON pm.pageid = ngi.id
                                                WHERE pm.nodeid IN (".implode(',',$children).") 
                                                AND pm.wf_state = 1
                                                AND pm.pagetype = " . $this->galleryImageType);
            
            $pager = new pager($total,$GLOBALS['_CONFIG']['perpage'],$_GET['pg']);
            
            // get images for this gallery folder
            /*
                the call to get the full path to the node may be
                a bit costly. This procedure may need refactoring
            */
            $images   = $this->db->getAll("SELECT 
                                                    pm.title            AS title,
                                                    ngi.content         AS content,
                                                    ngi.image           AS image,
                                                    get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                                    'files/pages/'||pm.pagetype||'__'||pm.pageid||'/image/' AS imagePath,
                                                    af.filename         AS imageFilename,
                                                    af.id               AS imageID
                                                FROM pagemap AS pm 
                                                LEFT JOIN node_gallery_image AS ngi ON pm.pageid = ngi.id
                                                LEFT JOIN added_files AS af ON pm.id = af.pageid
                                                WHERE pm.nodeid IN (".implode(',',$children).") 
                                                AND pm.pagetype = " . $this->galleryImageType . "
                                                AND pm.wf_state = 1
                                                ORDER BY pm.published DESC
                                                LIMIT ".$GLOBALS['_CONFIG']['perpage']." OFFSET ".$_GET['offset']);  
      
            //get folder properties if any
            $data = $this->db->getRow("SELECT * FROM node_gallery_folder WHERE id = ".$this->object);
            $data['title'] = $this->db->getOne("SELECT title FROM objectmap WHERE nodeid = ".$this->node);

            //get actual folder data
            //assign all this stuff to smarty
            $this->smarty->assign(array(
                                    'childFolders'  => $subcats,
                                    'total'         => $total,
                                    'images'        => $images,
                                    'pager'         => $pager->getPager()));

        }
        
        /**
         * set the action template
         * 
         * @return void
         **/
        public function setTemplate(){ 
            $this->setActionTemplate('gfolder.html');
        }
        
        
        /**
         * Sets the ID of the pagetype corresponsing to an gallery image
         *
         */
        private function setGalleryImageType(){
            $this->galleryImageType = (int)$this->db->getOne("SELECT id FROM datatype WHERE codeset = 'gimage'");
            if($this->galleryImageType == 0){
                throw new Exception("No Gallery Image Datatype Registered with the system!");   
            }
        }
    }
?>
